<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%String path = request.getContextPath() ;%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %> 
<%@ taglib uri="/WEB-INF/tld/ui.tld" prefix="ui" %> 
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>还款申请</title>
<jsp:include page="../../import.jsp" flush="true" />
<%--<jsp:include page="assParkCommon.jsp" flush="true" /><!-- 时间格式转换 -->--%>
</head>
<body class="iframeBody">
<div class="Wrapper">
    <div>
        <form id="repay_form">
            <div class="sqPanel">
                <div class="sqPanel_head inL">
                    <span>还 款 申 请</span>
                </div>
            </div>
            <!-- 列表显示块 -->
            <div id="ligergrid1" class="sq-grid"></div>
            <div class="fieldBox col_50" align="right">
                <div class="label requiredSymbol"  >剩余未还金额(元):
                    <span class="requiredSymbol" id="arAmts" name="arAmts"> </span>
                </div>
            </div>


            <div class="sqPanel" style="margin-top: 15px">
                <div class="sqPanel_head inL">
                    <div class="btnsBox inLC">
                        <span id="addBtn" class="sq-button sq-button-round">添加还款</span>
                        <span id="delBtn" class="sq-button sq-button-round">删除还款</span>
                    </div>
                    <span>还 款 信 息</span>
                </div>
                <!-- 列表显示块 -->
                <div id="ligergrid_repay" class="sq-grid"></div>

                <div class="fieldset1 tabel_col clearFix" style="margin-top: 15px">
                    <div class="fieldBox col_100">
                        <div class="label">附件上传</div>
                        <div class="field">
                            <jsp:include page="../../system/sysUlFilesRec/sysSysUlFilesRecUpload2.jsp" flush="true" >
                                <jsp:param name="filePath" value="/repay"/>
                            </jsp:include>
                            <%--<input  id="pkId"  name="pkId"  type="hidden"  value="${dtlId}">--%>
                        </div>
                    </div>

                    <div class="fieldBox col_100">
                        <div class="label">备注</div>
                        <div class="field">
                            <textarea name="repayRem" id="repayRem" rows="6" class="ui-textarea l-textarea" style="width:80%" value="" validate="{maxlength:200}"></textarea>
                        </div>
                    </div>
                </div>

                <!-- 列表显示块 -->
                <div id="ligergrid_his" class="sq-grid"></div>

            </div>
            <div class="btnsBox">
                <span id="saveBtn" class="sq-button sq-button-round" onclick="saveInfo()">提交还款申请</span>
                <span id="closeBtn" class="sq-button sq-button-round" onclick="Remove_navTab()">取消</span>
            </div>
            <%--还款（付款）ids--%>
            <input name="paydtlRIds" id="paydtlRIds" type="hidden" value="${paydtlRIds}">
        </form>
    </div>
</div>

<script type="text/javascript">
    var repayGrid = null;
    var rmtGrid = null;
    //处理下拉重置
    var beforeMyComBkCd = '';
    var afterMyComBkCd = '';
    //我司收款账号
    var myComAccListJSON = [];

	$(function () {
	    initGrid();//初始还款列表
        initGrid_repay()//初始还款信息
        // initGrid_his();//初始还款历史列表
        $('#repay_form').ligerForm({
            validate : true
        });
        //添加还款 和删除还款
        $("#addBtn").click(function (event) {
            rmtGrid.addRow({
                repayAmt: '',
                repayAccNm: '',
                repayAccNo: '',
                repayTm: '',
                acceptBkNm:'',
                acceptAccNo:''
            });
        });
        $("#delBtn").click(function (event) {
            var selectedRows = rmtGrid.getSelectedRows();
            if(selectedRows.length == 0){
                $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_SELECT_MORE_ROW , SQ_type: "warning" });
                return;
            }
            rmtGrid.deleteSelectedRow();
        });
	});

	//新增还款信息等
	function saveInfo() {
        var repay_form = liger.get("repay_form");

        var isValid = SQ.validGrid('ligergrid1');
        var repayIsValid = SQ.validGrid('ligergrid_repay');

        if (isValid == false) {
            $.ligerDialog.SQalert({content: "请维护使用金额！" , SQ_type: "warning" });
            return false;
        }
        if (repayIsValid == false) {
            $.ligerDialog.SQalert({content: "请维护还款信息！" , SQ_type: "warning" });
            return false;
        }
        var data = repay_form.getData();

        data["repayInfo"] = JSON.stringify(repayGrid.getData());//还款明细
        data["rmtInfo"] = JSON.stringify(rmtGrid.getData());//打款记录
        if (rmtGrid.getData().length < 1) {
            $.ligerDialog.SQalert({content: "请维护还款信息！" , SQ_type: "warning" });
            return false;
        }

        var fileParams = getUploadResultParams();//获取附件上传的信息
        data["fileParams"] = JSON.stringify(fileParams);
        var wait_dialog = null;
        $.ajax({
            type: 'post',
            dataType: 'json',
            data: data,
            url: '<%=path%>/finance/finRepayApplyInfo/add.do',
            beforeSend: function(){
                wait_dialog=$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING ,SQ_type: "waitting" });
            },
            success:function(msg){
                wait_dialog.close();
                if(msg.success == 1){
                    $.ligerDialog.SQalert({content:msg.info, SQ_type: "success"});
                    Remove_navTab();
                }else{
                    $.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
                }
            }
        });
    }

	//初始化列表
	function initGrid(){
        var paydtlRIds = $("#paydtlRIds").val();
        $("#ligergrid1").ligerGrid({
            // data: data,
            method:'get',
            url:'<%=path%>/finance/finRepayApplyInfo/selectPayInfo.do?paydtlRIds='+paydtlRIds,
            columns:[
                { display: '借款人/工号', name: 'cstMgrNmAndCstMgrCd', width: 150, enabledSort:true,},
                { display: '付款单号', name: 'channelNm', width: 150, enabledSort:true,
                    render: function (rowdata, rowindex, value, column) {
                        var h ="";
                        if( rowdata.pNo != null){
                            h+='<a class="sq-button-hasicon" href="javascript:commShowFlow(';
                            h+="'"+rowdata.pInstId+"'";
                            h+=",'"+rowdata.audStsCd+"'";
                            h+=",'"+rowdata.dataId+"'";
                            h+=",'"+rowdata.formUrl+"'";
                            h+=")\"><i class='sqBtnIco_32'></i>"+rowdata.pNo+"</a>";
                        }
                        return h;
                    }
                },
                { display: '费用项', name: 'loanTypCdStr', width: 100, enabledSort:true,},
                { display: '实际借款金额(元)', name: 'payAmt', width: 120, enabledSort:true,
                    render:function (rowdata, rowindex, value, column){
                      return value/100;
                    }
                },
                { display: '承租人', name: 'cstNm', width: 100, enabledSort:true},
                { display: '支付表号', name: 'paySchNo', width: 150, enabledSort:true},
                { display: '使用金额(元)(<span class="c_red">*</span>)', name: 'repayAmt', width: 150, enabledSort:true,
                    editor: {type: 'number',precision:2,
                        toFix:true,
                        onToFix:function(){
                            top.$.ligerDialog.SQalert({SQ_type: "warning",content: "小数点后面最多2位小数！"});
                        }
                    },
                    validate: function (value) {
                        if (StringUtils.isEmpty(value)) return "请填写使用金额！";
                    }
                },
                getGridOperColumnContent()//操作列
            ],
            onAfterShowData :function (e){
                var rows = e.rows;
                var i = 0;
                for ( var index in rows) {
                    i += rows[index].arAmt;
                }
                $("#arAmts").text(i);
            },
            usePager: false,//是否有分页便签
            root:'rows',
            record:'total',
            hideLoadButton:false,
            width:'100%',
            height:'100%',
            heightDiff:-2,
            isScroll: false,//是否有滚动条
            checkbox:false,//列表是否有选择框
            enabledEdit: true,//是否能编辑
            rownumbers:true//列表是否有行号
        });
        repayGrid = liger.get("ligergrid1");
        //grid.setHeight(100);
        repayGrid.options.isSingleCheck = true;//列表是否为单选
	}

    //操作列
    function getGridOperColumnContent(){
        var data = {
            display:'还款历史',
            isSort:false,
            width:150,
            align:'center',
            render:function (rowdata, rowindex, value){
                var h="";
                h+='<a class="sq-button-hasicon" alt="还款历史" href="javascript:getrepayHisRow(';
                h+="'"+rowdata.paySchDtlId+"'";
                h+=")\"><i class=\"sqBtnIco_4\"></i>"+"还款历史"+"</a>";

                return h;
            }
        };
        return data;
    }

    //根据 支付明细id获取历史还款
    function getrepayHisRow(paySchDtlId){
        //回调刷新列表
        /*var callback = function () {
            grid.reload();
        };*/
        if(!paySchDtlId){
            return;
        }//ID为空不执行
        Add_navTab('repay_his'+paySchDtlId,'还款申请记录','<%=path%>/finance/finRepayApplyInfo/toRepayHisPage.do?paySchDtlId='+paySchDtlId);
    }

    //初始化列表 历史还款
    function initGrid_his(){
        var paydtlRIds = $("#paydtlRIds").val();
        $("#ligergrid_his").ligerGrid({
            // data: data,
            method:'get',
            url:'<%=path%>/finance/finRmtRec/selectRepayHis.do?paydtlRIds='+paydtlRIds,
            columns:[
                { display: '还款金额(元)', name: 'repayAmt', width: 150, enabledSort:true,
                    render:function (rowdata, rowindex, value, column){
                        return value/100;
                    }
                },
                { display: '账款账户', name: 'repayAccNm', width: 150, enabledSort:true},
                { display: '账款账号', name: 'repayAccNo', width: 200, enabledSort:true},
                { display: '交易时间', name: 'repayTm', width: 150, enabledSort:true, render:renderDate},
                { display: '资金编号', name: 'repayAccNo', width: 200, enabledSort:true},

            ],
            usePager: false,//是否有分页便签
            root:'rows',
            record:'total',
            hideLoadButton:false,
            width:'100%',
            height:'100%',
            heightDiff:-2,
            isScroll: false,//是否有滚动条
            checkbox:false,//列表是否有选择框
            // enabledEdit: true,//是否能编辑
            rownumbers:true//列表是否有行号
        });
        grid = liger.get("ligergrid_his");
        //grid.setHeight(100);
        grid.options.isSingleCheck = true;//列表是否为单选
    }


    function initGrid_repay(){
        //我司收款银行
        var myComBankListJSON = [];
        <c:if test="${not empty myComBankListJSON}"> myComBankListJSON = ${myComBankListJSON}; </c:if>
        // //我司收款账号
        // var myComAccListJSON = [];
        <%--<c:if test="${not empty myComAccListJSON}"> yesOrNoCdListJSON = ${myComAccListJSON}; </c:if>--%>
        rmtGrid = $("#ligergrid_repay").ligerGrid({
            <%--url: '<%=path%>/project/prjQuatPrpsCfgD/initList.do',--%>
            method: 'get',
            columns: [
                {display: '还款金额(元)(<span class="c_red">*</span>)', name: 'repayAmt', minWidth: 80, type: 'text',
                    editor: {type: 'number',precision:2,
                        toFix:true,
                        onToFix:function(){
                            top.$.ligerDialog.SQalert({SQ_type: "warning",content: "小数点后面最多2位小数！"});
                        }
                    },
                    validate: function (value) {
                        if (StringUtils.isEmpty(value)) return "请填写还款金额！";
                    }

                },
                {display: '还款账户(<span class="c_red">*</span>)', name: 'repayAccNm', minWidth: 80, type: 'text',
                    editor: {type: 'text'},
                    validate: function (value) {
                        if (StringUtils.isEmpty(value)) return "请填写还款账户！";
                    }
                },
                {display: '还款账号(<span class="c_red">*</span>)', name: 'repayAccNo', minWidth: 80, type: 'text',
                    editor: {type: 'text'},
                    validate: function (value) {
                        if (StringUtils.isEmpty(value)) return "请填写还款账号！";
                    }
                },
                {
                    display: '交易时间(<span class="c_red">*</span>)', name: 'repayTm',
                    editor: {type: 'date'},
                    validate: function (value) {
                        if (StringUtils.isEmpty(value)) return "请填写还款账号！";
                    }
                },
                {display: '我司收款银行', name: 'acceptBkCd', minWidth: 100,
                    editor: {
                        type: 'select',
                        data: myComBankListJSON,
                        valueField: 'key',
                        textField: 'value'
                    },
                    valueField: 'acceptBkCd',
                    textField: 'acceptBkNm',
                    render: function (item) {
                        return item.acceptBkCd;
                    }
                },
                {display: '我司收款账号', name: 'acceptBranchNm', minWidth: 100,
                    editor: {
                        type: 'select',
                        data: myComAccListJSON,
                        valueField: 'key',
                        textField: 'value'
                    },
                    valueField: 'acceptBranchNm',
                    textField: 'acceptAccNo',
                    render: function (item) {
                        return item.acceptAccNo;
                    }
                },

            ],
            //编辑之前
            onBeforeEdit: function (e) {
                //处理我司银行账户
                if (e.column.columnname == "acceptBranchNm") {
                    getMyComAccListJSON(e.record.acceptBkCd);
                    e.column.editor.data=myComAccListJSON;
                }
                if (e.column.columnname == "acceptBkCd") {
                    beforeMyComBkCd = e.value;
                }
            },
            onBeforeSubmitEdit: function (e) {
                if (e.column.columnname == "acceptBkCd") {
                    afterMyComBkCd = e.value;
                }
                if (e.selected && e.column.columnname == "acceptBkCd" && beforeMyComBkCd!=afterMyComBkCd) {
                    e.record.acceptAccNo = null;
                    e.record.acceptBranchNm = null;
                }
                beforeMyComBkCd = '';
                afterMyComBkCd = '';
            },
            usePager: false,
            root: 'rows',
            record: 'total',
            hideLoadButton: true,
            width: '100%',
            height: 300,
            heightDiff: -2,
            enabledEdit: true,
            isScroll: false,
            checkbox: true,
            rownumbers: true,
            allowUnSelectRow: true,
            alternatingRow: false
         });
    }

    //根据我司银行 获取对应的账号
    function getMyComAccListJSON(acceptBkCd){
        if(!acceptBkCd){
            top.$.ligerDialog.SQalert({content: "请先选择我司收款银行", SQ_type: "warning"});
            return;
        }
        $.ajax({
            type: 'post',
            dataType:'json',
            async: false,
            data:{
                acceptBkCd:acceptBkCd
            },
            url: '<%=path%>/finance/finRepayApplyInfo/getMyComAccListJSON.do',
            beforeSend: function () {
                wait_dialog = $.ligerDialog.SQalert({
                    content: LogUtils.LOG_CMM_DATA_LOADING,
                    SQ_type: "waitting"
                });
            },
            success:function(msg){
                wait_dialog.close();
                if(msg.success == 1){
                    myComAccListJSON=msg.data;
                }
            }
        });
    }

    //显示流程：流程实例ID,状态,业务主键(借款)
    function commShowFlow(_pInstId, _audStsCd, _businessKey,_formUrl){
        if(StringUtils.isEmpty(_pInstId)){
            UIUtils.alertWarning(CommonConstant.C_P_INSTANCEID_NOT_HERE);
            return;
        }
        var params = "?pInsId="+_pInstId+"&status="+_audStsCd+"&businessKey="+_businessKey+"&formUrl="+_formUrl;
        Add_navTab('tab_ass_trail_flow_'+_businessKey,'查看流程',"<%=path%>/flow/flow/toFlowShowPageFowBus.do"+params);
    }


</script>
</body>
</html>